<?php

/**
 * Files component
 *
 */
class FilesComponent extends Object
{
	public $controller;
	public $maxsize = 1000;
	public $allowedImageExtensions = array("jpg", "jpeg", "gif", "png");
	
	function initialize(&$controller, $settings = array()) {
        // saving the controller reference for later use
        $this->controller =& $controller;
    }
 
    //called after Controller::beforeFilter()
    function startup(&$controller) {
               }
 
    //called after Controller::beforeRender()
    function beforeRender(&$controller) {
    }
 
    //called after Controller::render()
    function shutdown(&$controller) {
    }
 
    //called before Controller::redirect()
    function beforeRedirect(&$controller, $url, $status=null, $exit=true) {
    }
 
    function redirectSomewhere($value) {
        // utilizing a controller method
    }
	
	public function isValidFileType($fileName) {
		$extension = pathinfo($fileName, PATHINFO_EXTENSION);
		if (in_array($extension, $this->allowedImageExtensions)) {
			return true;
		} 
		return false;
	}
	public function uploadFile($dir, $fileTmp, $path, $fileName){
		$name = $this ->randomFileName($fileName);
		$path = $path . $name;
		move_uploaded_file($fileTmp,$path);
		return $dir . $name;
	}
	public function randomFileName($fileName){
		//$strTime = $this->incrementalHash();
		$strTime = uniqid("img_");
		$extension = pathinfo($fileName, PATHINFO_EXTENSION);
		$name = $strTime . "." . $extension; 
		return $name;
	}
	function incrementalHash($len = 5){
		  $charset = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
		  $base = strlen($charset);
		  $result = '';
		   $arr = explode(' ', microtime());
		  $now1 = explode(".",$arr[0]);
		  $now = $now1[1];
		  while ($now >= $base){
		    $i = $now % $base;
		    $result = $charset[$i] . $result;
		    $now /= $base;
		  }
		  return substr($result, -$len).$now1[1];
	}
	public function deleteFile($filePath){
		$file = new File($filePath);
		$file->delete();
		$file->close();
	}
	public function saveMultiFile($arrFile){
		$arrName =array();
		$i = 0;
		foreach($arrFile as $file){
			if($file['error']==0){
				$arrName[$i] = $this->uploadFile(DIR_PRODUCT, $file['tmp_name'], IMG_PRODUCT, $file['name']);
				$i++;
			}
		}
		return $arrName;
	}
	public function saveDetailsImg($arrFile, $productId){
		$imgList= array();
		$i=0;
		$arrFileNew = array();
		//CHECK FILE TYPE
		foreach($arrFile as $file){
			if($file['error']==0){
				if($this->isValidFileType($file['name']) == true){
					$arrFileNew[$i++] = $file;
				}
			}
		}
		//UPLOAD FILES
		$i = 0;
		$arrName = $this -> saveMultiFile($arrFileNew);
		foreach($arrName as $name){
			$imgList[$i]['product_id'] = $productId;
			$imgList[$i]['img'] = $name;
			$i++;
		}
		return $imgList;
	}
	public function updateDetailsImg($arrFile, $productId){
		$imgList= array();
		$i=0;
		$arrFileNew = array();
		$arrUpdate = array();
		foreach($arrFile as $file){
			if($file['img']['error']==0){
				if($this->isValidFileType($file['img']['name']) == true){
					$arrFileNew[$i] = $file['img'];
					$arrUpdate[$i] = $file;
					if($file['id']!=""){
						//delefile
						$this -> deleteFile(WWW_ROOT.$file['imgOld']);
					}
					$i++;
				}
			}
		}
		//
		//UPLOAD FILES
		$i = 0;
		$arrName = $this -> saveMultiFile($arrFileNew);
		foreach($arrName as $name){
			$imgList[$i]['product_id'] = $productId;
			$imgList[$i]['img'] = $name;
			$imgList[$i]['id'] = $arrUpdate[$i]['id'];
			$i++;
		}
		return $imgList;
	}
}
?>